package com.yanqu.road.server.timer.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.UnionQunyingTimeInfo;
import com.yanqu.road.entity.activity.unionwar.result.CurrentTimeInfo;
import com.yanqu.road.entity.unionwar.GuessUnionInfo;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarGuessDataListModel;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarActivityMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarGuessMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.timer.GeneralAgentTimerMgr;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * //同步竞猜数据（热度）
 */
public class SyncUnionWarGuessTask extends GeneralTask {

    private boolean isRun = false;

    private long lastCheckTime;
    @Override
    public void doRun() {
        if (!isRun) {
            isRun = true;
            long nowTime = System.currentTimeMillis();
            List<CrossUnionWarGuessDataListModel> syncList = new ArrayList<>();
            Map<Integer, ActivityInfo> activityInfoMap = CrossUnionWarActivityMgr.getOpenActivityMap();
            if (nowTime - lastCheckTime > GeneralAgentTimerMgr.ALL_SYNC_TIME) {
                for (ActivityInfo activityInfo : activityInfoMap.values()) {
                    CurrentTimeInfo currentTimeInfo = CrossUnionWarActivityMgr.getCurrentTimeInfo(activityInfo.getActivityId());
                    UnionQunyingTimeInfo timeInfo = currentTimeInfo.getTimeInfo();
                    if (timeInfo == null && currentTimeInfo.getPeriodType() == null) { //领奖期还是得同步（避免区服重启）
                        continue;
                    }
                    int phase = 2;
                    if(timeInfo != null) {
                        phase = timeInfo.getType();  //获取当前阶段
                    }
                    List<Integer> groupIdList = CrossUnionWarMgr.getGroupListByCrossServerId(activityInfo.getActivityId(), CrossBaseServer.getInstance().getServerId());
                    for (int i = 1 ; i <= phase; i++) {
                        for (int groupId : groupIdList) {
                            CrossUnionWarGuessDataListModel guessDataListModel = CrossUnionWarGuessMgr.getCrossUnionWarGuessDataListModel(activityInfo.getActivityId(), i, groupId);
                            if (guessDataListModel != null) {
                                syncList.add(guessDataListModel);
                            }
                        }
                    }
                }
                lastCheckTime = nowTime;
            } else {
                for (ActivityInfo activityInfo : activityInfoMap.values()) {
                    UnionQunyingTimeInfo timeInfo = CrossUnionWarActivityMgr.getCurrentTimeInfo(activityInfo.getActivityId()).getTimeInfo();
                    if (timeInfo == null) {
                        continue;
                    }
                    List<Integer> groupIdList = CrossUnionWarMgr.getGroupListByCrossServerId(activityInfo.getActivityId(), CrossBaseServer.getInstance().getServerId());
                    int phase = timeInfo.getType();  //获取当前阶段
                    for (int groupId : groupIdList) {
                        CrossUnionWarGuessDataListModel guessDataListModel = CrossUnionWarGuessMgr.getCrossUnionWarGuessDataListModel(activityInfo.getActivityId(), phase, groupId);
                        if (guessDataListModel != null && guessDataListModel.isNeedSync()) {
                            syncList.add(guessDataListModel);
                        }
                    }
                }
            }
            if (syncList.size() > 0) {
                for (CrossUnionWarGuessDataListModel guessDataListModel : syncList) {
                    UnionWarActivityServerProto.UnionWarGuessSyncMsg.Builder syncMsg = UnionWarActivityServerProto.UnionWarGuessSyncMsg.newBuilder();
                    syncMsg.setActivityId(guessDataListModel.getActivityId());
                    syncMsg.setStage(guessDataListModel.getPhase());
                    for (GuessUnionInfo guessUnionInfo : guessDataListModel.getGuessUnionInfoMap().values()) {
                        UnionWarActivityServerProto.UnionWarGuessTemp.Builder unionWarGuessTemp = UnionWarActivityServerProto.UnionWarGuessTemp.newBuilder();
                        unionWarGuessTemp.setUnionUid(guessUnionInfo.getUnionUid());
                        unionWarGuessTemp.setUnionBaseTemp(UnionBasePb.parseUnionBaseTempMsg(guessUnionInfo.getUnionBaseInfo()));
                        unionWarGuessTemp.setPopularity(guessUnionInfo.getGuessPopularity());
                        unionWarGuessTemp.setGroupId(guessDataListModel.getGroupId());
                        syncMsg.addUnionWarGuess(unionWarGuessTemp);
                    }
                    guessDataListModel.setNeedSync(false);
                    syncToGameServer(syncMsg, activityInfoMap.get(guessDataListModel.getActivityId()), guessDataListModel.getGroupId());
                }
            }
            isRun = false;
        }
    }

    private void syncToGameServer(UnionWarActivityServerProto.UnionWarGuessSyncMsg.Builder syncMsg, ActivityInfo activityInfo, int groupId) {
        if (activityInfo == null || syncMsg == null) {
            return;
        }
        for (long serverId : CrossUnionWarMgr.getServerListByGroupId(activityInfo, groupId)) {
            MessageHelper.sendPacket(serverId, 0, YanQuMessageUtils.buildMessage(GameProtocol.S_UNION_WAR_SYNC_GUESS, syncMsg));
        }
    }
}
